﻿using NPOI.POIFS.NIO;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using Telerik.Windows.Documents.RichTextBoxCommands;
using Unity;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class DrawingDocViewModel : RegionViewModelBase
    {
        public IEventAggregator eventAggregator1 { get; set; }
        public IDrawingService dwgServ { get; set; }
        public ICodeListService codeListService { get; set; }
        private ObservableCollection<Drawing> _dataSource;

        public ObservableCollection<Drawing> dataSource
        {
            get { return _dataSource; }
            set { SetProperty(ref _dataSource, value); }
        }
        //private ObservableCollection<DrawingType> _docTypeSource;

        //public ObservableCollection<DrawingType> docTypeSource
        //{
        //    get { return _docTypeSource; }
        //    set { _docTypeSource = value; }
        //}
        //private DrawingType _selectedDocType;

        //public DrawingType selectedDocType
        //{
        //    get { return _selectedDocType; }
        //    set { _selectedDocType = value; }
        //}

        //private ICommand _docTypeSelectedCommand;

        //public ICommand docTypeSelectedCommand
        //{
        //    get
        //    {
        //        if (_docTypeSelectedCommand != null)
        //        {
        //            return _docTypeSelectedCommand;
        //        }
        //        _docTypeSelectedCommand = new DelegateCommand<object>(OnDocTypeSelectedCommand);
        //        return _docTypeSelectedCommand;
        //    }
        //}

        //private void OnDocTypeSelectedCommand(object obj)
        //{

        //}

        private ICommand _saveCommand;

        public ICommand saveCommand
        {
            get
            {
                if (_saveCommand != null)
                {
                    return _saveCommand;
                }
                _saveCommand = new DelegateCommand<object>(OnsaveCommand);
                return _saveCommand;
            }
        }
        
 
        private async void OnsaveCommand(object obj)
        {

            var task1 = Task.Run(async () =>
            {
                await dwgServ.UpdateDwgs(GlobalObject.CurrentProject.ProjId, dataSource.Where(x => x.IsModify).ToList());
            });
            await task1;

            foreach (var item in dataSource)
            {
                item.IsModify = false;
            }
            Dialog.ShowMessage("保持成功！");
        }

        public IDynamicDataApi _dynamicDataApi { get; set; }
        public DrawingDocViewModel(
            IRegionManager regionManager,
            IUnityContainer unityContainer,
            IDynamicDataApi dynamicDataApi,
            IDialogService dialogService,
            IEventAggregator eventAggregator) : base(regionManager, unityContainer, dialogService)
        {
            eventAggregator1 = eventAggregator;
            _dynamicDataApi = dynamicDataApi;
            //var columns = GlobalObject.DrawingDocPropertyInfos;
            codeListService = unityContainer.Resolve<ICodeListService>();
            dwgServ = unityContainer.Resolve<IDrawingService>();


        }

        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            IsBusy = true;

            if (navigationContext.Parameters.ContainsKey("Title"))
                Title.TitleText = navigationContext.Parameters["Title"].ToString();
            if (navigationContext.Parameters.ContainsKey("TitleGlyph"))
                Title.TitleGlyph = navigationContext.Parameters["TitleGlyph"].ToString();
            RaisePropertyChanged(nameof(Title));

            var dataStructObj = await _dynamicDataApi.GetDrawingDocDynamicsAsync(GlobalObject.CurrentProject.ProjId.ToString(), 0, 0);
            dataSource = new ObservableCollection<Drawing>();
            dataSource.AddRange(dataStructObj.Data);


            IsBusy = false;
        }
        private DelegateCommand<object> _CellEditEndedCommand;
        public ICommand CellEditEndedCommand
        {
            get
            {
                if (_CellEditEndedCommand != null)
                {
                    return _CellEditEndedCommand;
                }
                _CellEditEndedCommand = new DelegateCommand<object>(OnCellEditEndedCommandAsync);
                return _CellEditEndedCommand;
            }
        }
        /// <summary>
        /// 单元格编辑结束的处理命令
        /// </summary>
        /// <param name="obj"></param>
        void OnCellEditEndedCommandAsync(object obj)
        {
            var e = obj as GridViewCellEditEndedEventArgs;
            if (e == null) return;
            var enumObj = e.Cell.DataContext;
            if (enumObj == null)
            {
                return;
            }
            var enumObj2 = (Drawing)enumObj;
            enumObj2.IsModify = true;
            //if (dropVal != null)
            //{
            //    return;
            //}
            //else
            //{
            //    var pRow = e.Cell.ParentRow.DataContext;
            //    Reflect.SetPropValue(pRow, tag.v1, e.OldData);
            //    return;
            //}
        }

        private RadObservableCollection<GroupDescriptor> _groupDescriptors;
        /// <summary>
        /// 获取或设置分组描述列。
        /// </summary>
        public RadObservableCollection<GroupDescriptor> GroupDescriptors
        {
            get
            {
                //viewmodel初始化时，会自动进来
                if (_groupDescriptors != null)
                    return _groupDescriptors;
                _groupDescriptors = GetGroupDef();
                return _groupDescriptors;
            }
            set
            {
                SetProperty(ref _groupDescriptors, value);
            }
        }

        private RadObservableCollection<GroupDescriptor> GetGroupDef()
        {
            var groupDescriptors = new RadObservableCollection<GroupDescriptor>();
            groupDescriptors.SuspendNotifications();//暂时不要通知

            var x = new Drawing();
            var gridViewColumn = new GroupDescriptor
            {
                Member = nameof(x.dwgTypeName),
                DisplayContent = "图纸类型",
            };
            groupDescriptors.Add(gridViewColumn);

            groupDescriptors.ResumeNotifications();//恢复通知
            return groupDescriptors;
        }


        /// <summary>
        /// 根据动态属性集合获取分组描述列。
        /// </summary>
        /// <returns></returns>
        protected RadObservableCollection<GroupDescriptor> GetGroupDescriptors(IList<DynamicPropertyInfo> list)
        {
            var group = from v in list
                        where v.IsGroup
                        select v;
            if (group != null && group.Count() > 0)
            {
                var groupDescriptors = new RadObservableCollection<GroupDescriptor>();
                groupDescriptors.SuspendNotifications();
                foreach (var item in group)
                {
                    var gridViewColumn = new GroupDescriptor
                    {
                        Member = item.PropertyName,
                        DisplayContent = item.DisplayName,
                    };
                    groupDescriptors.Add(gridViewColumn);
                }
                groupDescriptors.ResumeNotifications();
                return groupDescriptors;
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        protected override async void OnInitPage(object obj)
        {

        }
        private bool _IsUpdating;

        public bool IsBusy
        {
            get { return _IsUpdating; }
            set
            {
                SetProperty(ref _IsUpdating, value);
            }
        }


    }
}
